#!/bin/bash

# 测试大数据量StackOverflowError修复效果
echo "=== 大数据量StackOverflowError修复测试 ==="
echo "测试时间: $(date)"
echo ""

# 测试参数
BASE_URL="http://localhost:8080"
API_ENDPOINT="/admin-api/system/college-recommendation/recommend"

# 测试用例1: 中等数据量测试（应该正常工作）
echo "1. 中等数据量测试"
echo "-----------------------------------"

MEDIUM_PARAMS="province=辽宁&score=550&ranks=25000&year=2024&classify=物理类&subjects=物理,化学&batch=本科批&pageNum=1&pageSize=100"

echo "发送中等数据量请求..."
START_TIME=$(date +%s%3N)
RESPONSE=$(curl -s -w "\n%{http_code}" -X GET "${BASE_URL}${API_ENDPOINT}?${MEDIUM_PARAMS}")
END_TIME=$(date +%s%3N)
DURATION=$((END_TIME - START_TIME))

HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
RESPONSE_BODY=$(echo "$RESPONSE" | head -n -1)

echo "HTTP状态码: $HTTP_CODE"
echo "响应时间: ${DURATION}ms"

if [ "$HTTP_CODE" = "200" ]; then
    echo "✅ 中等数据量测试通过"
    # 检查是否有推荐结果
    RESULT_COUNT=$(echo "$RESPONSE_BODY" | grep -o '"total":[0-9]*' | grep -o '[0-9]*')
    echo "推荐结果数量: $RESULT_COUNT"
else
    echo "❌ 中等数据量测试失败"
    echo "响应内容: $RESPONSE_BODY"
fi
echo ""

# 测试用例2: 大数据量测试（之前会出现StackOverflowError）
echo "2. 大数据量测试"
echo "-----------------------------------"

LARGE_PARAMS="province=辽宁&score=500&ranks=40000&year=2024&classify=物理类&subjects=物理&batch=本科批&pageNum=1&pageSize=200"

echo "发送大数据量请求..."
START_TIME=$(date +%s%3N)
RESPONSE=$(curl -s -w "\n%{http_code}" -X GET "${BASE_URL}${API_ENDPOINT}?${LARGE_PARAMS}")
END_TIME=$(date +%s%3N)
DURATION=$((END_TIME - START_TIME))

HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
RESPONSE_BODY=$(echo "$RESPONSE" | head -n -1)

echo "HTTP状态码: $HTTP_CODE"
echo "响应时间: ${DURATION}ms"

if [ "$HTTP_CODE" = "200" ]; then
    echo "✅ 大数据量测试通过"
    RESULT_COUNT=$(echo "$RESPONSE_BODY" | grep -o '"total":[0-9]*' | grep -o '[0-9]*')
    echo "推荐结果数量: $RESULT_COUNT"
    
    # 检查是否有StackOverflowError相关的错误信息
    if echo "$RESPONSE_BODY" | grep -q "StackOverflowError"; then
        echo "⚠️  响应中仍包含StackOverflowError信息"
    else
        echo "✅ 未发现StackOverflowError"
    fi
else
    echo "❌ 大数据量测试失败"
    echo "响应内容: $RESPONSE_BODY"
fi
echo ""

# 测试用例3: 超大数据量测试（极限情况）
echo "3. 超大数据量测试"
echo "-----------------------------------"

XLARGE_PARAMS="province=辽宁&score=450&ranks=60000&year=2024&classify=物理类&subjects=&batch=本科批&pageNum=1&pageSize=500"

echo "发送超大数据量请求..."
START_TIME=$(date +%s%3N)
RESPONSE=$(curl -s -w "\n%{http_code}" -X GET "${BASE_URL}${API_ENDPOINT}?${XLARGE_PARAMS}")
END_TIME=$(date +%s%3N)
DURATION=$((END_TIME - START_TIME))

HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
RESPONSE_BODY=$(echo "$RESPONSE" | head -n -1)

echo "HTTP状态码: $HTTP_CODE"
echo "响应时间: ${DURATION}ms"

if [ "$HTTP_CODE" = "200" ]; then
    echo "✅ 超大数据量测试通过"
    RESULT_COUNT=$(echo "$RESPONSE_BODY" | grep -o '"total":[0-9]*' | grep -o '[0-9]*')
    echo "推荐结果数量: $RESULT_COUNT"
    
    # 检查响应时间是否合理
    if [ "$DURATION" -lt 30000 ]; then  # 30秒以内
        echo "✅ 响应时间合理"
    else
        echo "⚠️  响应时间较长: ${DURATION}ms"
    fi
else
    echo "❌ 超大数据量测试失败"
    echo "响应内容: $RESPONSE_BODY"
fi
echo ""

# 测试用例4: 连续请求测试（测试缓存和稳定性）
echo "4. 连续请求测试"
echo "-----------------------------------"

echo "发送5个连续请求..."
TOTAL_TIME=0
SUCCESS_COUNT=0

for i in {1..5}; do
    echo "请求 $i/5..."
    START_TIME=$(date +%s%3N)
    RESPONSE=$(curl -s -w "\n%{http_code}" -X GET "${BASE_URL}${API_ENDPOINT}?${MEDIUM_PARAMS}")
    END_TIME=$(date +%s%3N)
    DURATION=$((END_TIME - START_TIME))
    TOTAL_TIME=$((TOTAL_TIME + DURATION))
    
    HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
    if [ "$HTTP_CODE" = "200" ]; then
        SUCCESS_COUNT=$((SUCCESS_COUNT + 1))
        echo "  请求 $i: ✅ 成功 (${DURATION}ms)"
    else
        echo "  请求 $i: ❌ 失败 (${DURATION}ms)"
    fi
done

AVERAGE_TIME=$((TOTAL_TIME / 5))
echo "连续请求结果:"
echo "  成功次数: $SUCCESS_COUNT/5"
echo "  平均响应时间: ${AVERAGE_TIME}ms"
echo "  总耗时: ${TOTAL_TIME}ms"

if [ "$SUCCESS_COUNT" -eq 5 ]; then
    echo "✅ 连续请求测试通过"
else
    echo "❌ 连续请求测试失败"
fi
echo ""

# 总结
echo "=== 测试总结 ==="
echo "修复效果验证:"
echo "1. 中等数据量: 应该正常工作"
echo "2. 大数据量: 不应该出现StackOverflowError"
echo "3. 超大数据量: 应该使用极简查询策略"
echo "4. 连续请求: 应该稳定工作，后续请求更快（缓存效果）"
echo ""
echo "如果所有测试都通过，说明StackOverflowError问题已经修复。"
echo "如果仍有问题，请检查应用日志中的详细错误信息。"
echo ""
echo "建议监控指标:"
echo "- 查看日志中的查询策略选择信息"
echo "- 监控内存使用情况"
echo "- 观察批量查询统计信息"
